// pages/buy-vip/palceOrder.js
const app = getApp();
var http = require('../../utils/network.js');
var url = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: {},
    AddList:''
  },
  //微信支付
  wxPay: function(e) {
    var that = this
    var order_number = e.target.dataset.number
    var user_id = wx.getStorageSync('user_id') || ''
    var body = '购买'
    var goods = '购买'
    var detail = '购买'
    var total_fee = (e.target.dataset.price * 100).toFixed(0)
    // var total_fee = 1

    var wxpayOrderUrl = ''
    var param = {
      user_id: 1,
      body:body,
      goods: goods,
      detail: detail,
      total_fee: total_fee,
      order_number: order_number
    }
    var wxpayOrderResult = http.post(wxpayOrderUrl, param, true);
    wxpayOrderResult.then(function (res) {
      console.log(res)
      wx.requestPayment({
        'timeStamp': res.list.timeStamp,
        'nonceStr': res.list.nonceStr,
        'package': res.list.package,
        'signType': 'MD5',
        'paySign': res.list.paySign,
        'success': function (res) {
          if (res.errMsg == 'requestPayment:ok') {
            // 支付成功返回上一个页面
            wx.showModal({
              title: '提示',
              content: '支付成功',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.navigateBack({
                    delta: 1
                  })
                }
              }
            })
          }
        },
        'fail': function (res) {
          // 支付失败
          if (res.errMsg === 'requestPayment:fail cancel') {
            // 用户取消支付
            wx.showToast({
              title: '您已取消支付',
              icon: 'success',
              duration: 2000
            })
          } else {
            // 支付失败
            wx.showModal({
              title: '提示',
              content: '支付失败，请您稍后重试！',
              showCancel: false,
              success: function (res) {
              }
            })
          }
        },
        'complete': function (res) {
          console.log('other : ' + res.errMsg)
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})